package cac.fenjian.resource.AoTask.MsTaskPackage.rest;

import cac.common.CreateCommonLog;
import cac.fenjian.resource.AoTask.MsTaskPackage.dao.MsTaskCheckOperDao;
import cac.fenjian.resource.AoTask.MsTaskPackage.dto.MsTaskPackageDTO;
import cac.fenjian.resource.AoTask.MsTaskPackage.service.MsTaskCheckOperService;
import cac.fenjian.resource.AoTask.MsTaskPackage.service.MsTaskPackageService;
import cac.fenjian.resource.mssslshelves.service.MsSslShelvesService;
import cac.fenjian.resource.outStoreroom.dao.OutStoreroomDao;
import cac.fenjian.resource.outStoreroom.sevice.OutStoreroomService;
import com.alibaba.fastjson.JSON;
import com.cac.dme.adapter.rest.msg.PageParameter;
import com.cac.dme.adapter.rest.msg.QueryReqBean;
import com.cac.dme.adapter.rest.msg.QueryRespBean;
import com.cac.dme.adapter.rest.msg.ResponseMsg;
import com.cac.dme.adapter.utils.ContextCommonHolder;
import com.cac.dme.common.utils.JsonHelper;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

import static com.cac.dme.adapter.utils.BusinessUtil.getSortExpColumnName;

/**
 * @program test-curd
 * @description 工作包分拣任务服务接口
 * @author: LSL
 * @create: 2022-08-29-14-25
 **/
@RestController
@RequestMapping("/msTaskPackage")
@Slf4j
public class MsTaskPackageRest {

    @Autowired
    private MsTaskPackageService msTaskPackageService;
    @Autowired
    private MsSslShelvesService msSslShelvesService;
    @Autowired
    private CreateCommonLog createCommonLog;
    @Autowired
    private OutStoreroomService outStoreroomService;
    @Autowired
    private OutStoreroomDao outStoreroomDao;
    @Autowired
    private MsTaskCheckOperService msTaskCheckOperService;
    @Autowired
    private MsTaskCheckOperDao msTaskCheckOperDao;


    //配套计划条件查询
    @PostMapping("/searchByPage")
    public ResponseMsg<QueryRespBean<MsTaskPackageDTO>> searchByPage(@RequestBody Map map) {
        //时间范围
        String startTimeParams = JSON.toJSONString(map.get("startTime"));
        String endTimeParams = JSON.toJSONString(map.get("endTime"));
        Date startTime = JSON.parseObject(startTimeParams, Date.class);
        Date endTime = JSON.parseObject(endTimeParams, Date.class);
        ResponseMsg<QueryRespBean<MsTaskPackageDTO>> responseMsg = new ResponseMsg<>();
        String page = JSON.toJSONString(map.get("pageParameter"));
        PageParameter pageParameter = JSON.parseObject(page, PageParameter.class);
        String DTO = JSON.toJSONString(map.get("searchParams"));
        MsTaskPackageDTO searchParams = JSON.parseObject(DTO, MsTaskPackageDTO.class);
        QueryReqBean<MsTaskPackageDTO> queryReqBean = new QueryReqBean<>();
        queryReqBean.setPageParameter(pageParameter);
        queryReqBean.setSearchParams(searchParams);
        QueryRespBean<MsTaskPackageDTO> result = msTaskPackageService.searchByPage(queryReqBean, startTime, endTime);
        responseMsg.setResponseBody(result);
        return responseMsg;
    }

    //预配套条件查询
    @PostMapping("/searchByParams")
    public ResponseMsg<QueryRespBean<MsTaskPackageDTO>> searchByParams(@RequestBody Map map) {
        //时间范围
        String startTimeParams = JSON.toJSONString(map.get("startTime"));
        String endTimeParams = JSON.toJSONString(map.get("endTime"));
        Date startTime = JSON.parseObject(startTimeParams, Date.class);
        Date endTime = JSON.parseObject(endTimeParams, Date.class);
        ResponseMsg<QueryRespBean<MsTaskPackageDTO>> responseMsg = new ResponseMsg<>();
        String page = JSON.toJSONString(map.get("pageParameter"));
        PageParameter pageParameter = JSON.parseObject(page, PageParameter.class);
        String DTO = JSON.toJSONString(map.get("searchParams"));
        MsTaskPackageDTO searchParams = JSON.parseObject(DTO, MsTaskPackageDTO.class);
        QueryReqBean<MsTaskPackageDTO> queryReqBean = new QueryReqBean<>();
        queryReqBean.setPageParameter(pageParameter);
        queryReqBean.setSearchParams(searchParams);
        QueryRespBean<MsTaskPackageDTO> result = msTaskPackageService.searchByParams(queryReqBean, startTime, endTime);
        responseMsg.setResponseBody(result);
        return responseMsg;
    }

    //分拣操作工作包任务查询
    @PostMapping("/searchOperatePage")
    public ResponseMsg<QueryRespBean<MsTaskPackageDTO>> searchOperatePage(@RequestBody Map map) {
        String IP = null;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (request.getHeader("x-forwarded-for") == null) {
            IP = request.getRemoteAddr();
        } else {
            IP = request.getHeader("x-forwarded-for");
        }
        //时间范围
        String startTimeParams = JSON.toJSONString(map.get("startTime"));
        String endTimeParams = JSON.toJSONString(map.get("endTime"));
        Date startTime = JSON.parseObject(startTimeParams, Date.class);
        Date endTime = JSON.parseObject(endTimeParams, Date.class);
        ResponseMsg<QueryRespBean<MsTaskPackageDTO>> responseMsg = new ResponseMsg<>();
        String page = JSON.toJSONString(map.get("pageParameter"));
        PageParameter pageParameter = JSON.parseObject(page, PageParameter.class);
        String DTO = JSON.toJSONString(map.get("searchParams"));
        MsTaskPackageDTO searchParams = JSON.parseObject(DTO, MsTaskPackageDTO.class);
        QueryReqBean<MsTaskPackageDTO> queryReqBean = new QueryReqBean<>();
        queryReqBean.setPageParameter(pageParameter);
        queryReqBean.setSearchParams(searchParams);
        QueryRespBean<MsTaskPackageDTO> result = msTaskPackageService.searchOperatePage(queryReqBean, startTime, endTime, IP);
        responseMsg.setResponseBody(result);
        return responseMsg;
    }

    //分拣预配套工作包AO包查询
    @PostMapping("/searchPackageAoByPage")
    public ResponseMsg<QueryRespBean<MsTaskPackageDTO>> searchPackageAoByPage(@RequestBody Map map) {
        ResponseMsg<QueryRespBean<MsTaskPackageDTO>> responseMsg = new ResponseMsg<>();
        String page = JSON.toJSONString(map.get("pageParameter"));
        PageParameter pageParameter = JSON.parseObject(page, PageParameter.class);
        String DTO = JSON.toJSONString(map.get("searchParams"));
        MsTaskPackageDTO searchParams = JSON.parseObject(DTO, MsTaskPackageDTO.class);
        QueryReqBean<MsTaskPackageDTO> queryReqBean = new QueryReqBean<>();
        queryReqBean.setPageParameter(pageParameter);
        queryReqBean.setSearchParams(searchParams);
        QueryRespBean<MsTaskPackageDTO> result = msTaskPackageService.searchPackageAoByPage(queryReqBean);
        responseMsg.setResponseBody(result);
        return responseMsg;
    }

    //已配套工作包AO包查询
    @PostMapping("/searchSorted")
    public ResponseMsg<QueryRespBean<MsTaskPackageDTO>> searchSorted(@RequestBody Map map) {
        ResponseMsg<QueryRespBean<MsTaskPackageDTO>> responseMsg = new ResponseMsg<>();
        String page = JSON.toJSONString(map.get("pageParameter"));
        PageParameter pageParameter = JSON.parseObject(page, PageParameter.class);
        String DTO = JSON.toJSONString(map.get("searchParams"));
        MsTaskPackageDTO searchParams = JSON.parseObject(DTO, MsTaskPackageDTO.class);
        QueryReqBean<MsTaskPackageDTO> queryReqBean = new QueryReqBean<>();
        queryReqBean.setPageParameter(pageParameter);
        queryReqBean.setSearchParams(searchParams);
        QueryRespBean<MsTaskPackageDTO> result = msTaskPackageService.searchSorted(queryReqBean);
        responseMsg.setResponseBody(result);
        return responseMsg;
    }

    /**
     * 根据工作包编码，查询详细信息。
     * 包括：AO信息，AO中托盘信息，托盘物料，对应初始缓存箱信息
     *
     * @param map
     * @return
     */
    @PostMapping("/detailedAoInfo")
    public ResponseMsg<QueryRespBean> detailedAoInfo(@RequestBody HashMap map) {
        ResponseMsg responseMsg = new ResponseMsg<>();
        if (map != null) {
            String page = JSON.toJSONString(map.get("pageParameter"));
            PageParameter pageParameter = JSON.parseObject(page, PageParameter.class);
            QueryReqBean<HashMap> queryReqBean = new QueryReqBean<>();
            queryReqBean.setPageParameter(pageParameter);
            QueryRespBean result = msTaskPackageService.detailedAoInfo(queryReqBean);
            responseMsg.setRetCode("200");
            responseMsg.setResponseBody(result);
        } else {
            responseMsg.setRetCode("201");
            responseMsg.setMessage("数据参数为空！");
        }
        return responseMsg;
    }

    //工艺更改分拣
    @PostMapping("/searchChangePackage")
    public ResponseMsg<QueryRespBean<MsTaskPackageDTO>> searchChangePackage(@RequestBody Map map) {
        ResponseMsg<QueryRespBean<MsTaskPackageDTO>> responseMsg = new ResponseMsg<>();
        String page = JSON.toJSONString(map.get("pageParameter"));
        PageParameter pageParameter = JSON.parseObject(page, PageParameter.class);
        String DTO = JSON.toJSONString(map.get("searchParams"));
        MsTaskPackageDTO searchParams = JSON.parseObject(DTO, MsTaskPackageDTO.class);
        QueryReqBean<MsTaskPackageDTO> queryReqBean = new QueryReqBean<>();
        queryReqBean.setPageParameter(pageParameter);
        queryReqBean.setSearchParams(searchParams);
        QueryRespBean<MsTaskPackageDTO> result = msTaskPackageService.searchChangePackage(queryReqBean);
        responseMsg.setResponseBody(result);
        return responseMsg;
    }

    //工作包出库
    @PostMapping("/taskOut")
    public ResponseMsg<String> taskOut(@RequestBody HashMap hashMap) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        //获取IP
        String IP = null;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (request.getHeader("x-forwarded-for") == null) {
            IP = request.getRemoteAddr();
        } else {
            IP = request.getHeader("x-forwarded-for");
        }
        //操作位
        String operationBit = null;
        String boxType = "TOOL";
        //获取操作位置编号
        operationBit = msSslShelvesService.findStartCode(IP, boxType);
        if("".equals(operationBit)||operationBit==null){
            return responseMsg.setMessage("获取操作台位置异常，请配置操作台信息！");
        }
        String agv = (String) hashMap.get("agv");
        try {
            if (agv.equals("")) {
                //分拣操作出库工作包查询
                List<HashMap> mapList = msTaskPackageService.taskOut(hashMap);
                if (mapList.size() == 0) {
                    return responseMsg.setRetCode("201").setMessage("无法下达工作包出库命令！");
                }
                for (int i = 0; i < mapList.size(); i++) {
                    //得到托盘编码
                    HashMap map = mapList.get(i);
                    if (map.get("palletCode").toString().startsWith("2")) {
                        map.put("boxType", "TOOL");
                        map.put("taskType", "2");
                        map.put("command", "1");
                        map.put("IP", IP);
                        //获取操作位置编号
                        map.put("endCode", "xyssj" + operationBit);
                        //调用创建日志类
                        createCommonLog.outBound(map);
                    } else return responseMsg.setRetCode("201").setMessage("工作包条码格式错误！");
                }
                HashMap outFirst = new HashMap();
                //获取操作位
                operationBit = msSslShelvesService.findStartCode(IP, boxType);
                //结束位置及标识
                outFirst.put("endCode", "xyssj" + operationBit);
                outFirst.put("func", "sort");
                //调用流量控制类
                HashMap str = createCommonLog.callWCSInterface(outFirst);
                String s = (String) str.get("retCode");
                if (s.equals("200")) {
                    responseMsg.setRetCode("200").setMessage((String) str.get("msg"));
                } else responseMsg.setRetCode("201").setMessage((String) str.get("msg"));
            }
            if (agv.equals("agv")) {
                //配送出库工作包查询
                HashMap map = (HashMap) hashMap.get("queryParam");
                if (Objects.isNull(map)) {
                    return responseMsg.setRetCode("201").setMessage("无法下达工作包出库命令！");
                }
                //得到托盘和工作包编码
                HashMap out = outStoreroomDao.outStoreroomQuerySorting(map);
//                        outStoreroomService.outStoreroomWorkbin(map);
                if (map.get("toolCode").toString().startsWith("2")) {
                    //得到托盘和工作包编码
                    out.put("palletCode", map.get("toolCode"));
                    out.put("packageCode", map.get("packageCode"));
                    //组装需要的其他参数
                    out.put("boxType", "TOOL");
                    out.put("taskType", "2");
                    out.put("command", "1");
                    out.put("IP", IP);
                    out.put("callPriority", map.get("area"));
                    //目标位
                    out.put("func", "agv");
                    out.put("endCode", "agv");
                    //调用创建日志类
                    HashMap rsl=createCommonLog.outBound(out);
                    hashMap.put("retCode", "200");
                    hashMap.put("retMsg", "创建日志成功！");
                    responseMsg.setMessage(rsl.get("retMsg").toString()).setRetCode("retCode");
                } else {
                    return responseMsg.setRetCode("201").setMessage("工作包条码格式错误！");
                }

                HashMap outFirst = new HashMap();
                //结束位置，标识
                outFirst.put("endCode", "agv");
                outFirst.put("func", "agv");
                //调用流量控制类
                HashMap str = createCommonLog.callWCSInterface(outFirst);
                String s = (String) str.get("retCode");
                if (s.equals("200")) {
                    responseMsg.setRetCode("200").setMessage((String) str.get("msg"));
                } else responseMsg.setRetCode("201").setMessage((String) str.get("msg"));
            }
            return responseMsg;
        } catch (Exception e) {
            e.printStackTrace();
            return responseMsg.setRetCode("201");
        }
    }

    //工作包回库
    @PostMapping("/taskIn")
    public ResponseMsg<HashMap> taskIn(@RequestBody Map map) {
        ResponseMsg<HashMap> responseMsg = new ResponseMsg<>();
        String IP = null;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (request.getHeader("x-forwarded-for") == null) {
            IP = request.getRemoteAddr();
        } else {
            IP = request.getHeader("x-forwarded-for");
        }
        String operationBit = null;
        String boxType = "TOOL";

        //yebo 更改操作记录表标识
        // msTaskCheckOperService.updateMsConsoleOper("package","2");
        //6-26lqc
        String palletCode = (String) map.get("palletCode");
        if (("").equals(palletCode)) {
            return responseMsg.setRetCode("201").setMessage("无法下达回库命令！");
        }
        operationBit = msSslShelvesService.findStartCode(IP, boxType);
        if (map.get("palletCode") != null && !"".equals(map.get("palletCode"))) {
            try {
                HashMap in = new HashMap();
                in.put("palletCode", map.get("palletCode"));
                in.put("command", "5");
                in.put("boxType", "TOOL");
                in.put("startCode", operationBit);
                in.put("func", "sort");
                //调用创建出库日志
                String s = createCommonLog.inBound(in);
                if (s != "200") {
                    return responseMsg.setMessage(s).setRetCode("201");
                }

                HashMap outFirst = new HashMap();
                operationBit = msSslShelvesService.findStartCode(IP, boxType);
                outFirst.put("endCode", "xyssj" + operationBit);
                outFirst.put("func", "sort");
                outFirst.put("palletCode", map.get("palletCode"));
                //增加回库操作标签，用于向页面返回下一个AO
                outFirst.put("backTask", "backTool");
                outFirst.put("boxType", "TOOL");
                //调用流量控制
                HashMap hashMap = createCommonLog.callWCSInterface(outFirst);

                String str = (String) hashMap.get("retCode");
                if (str.equals("200")) {
                    responseMsg.setRetCode("200").setMessage((String) hashMap.get("msg"));
                    responseMsg.setResponseBody(hashMap);
                } else responseMsg.setRetCode("201").setMessage((String) hashMap.get("msg"));

                return responseMsg;
            } catch (Exception e) {
                e.printStackTrace();
                return responseMsg.setRetCode("201");
            }
        } else {
            responseMsg.setRetCode("201");
            responseMsg.setMessage("无法下达回库命令！");
            return responseMsg;
        }
    }

    //工作包物料信息查询
    @PostMapping("/searchPackageInfo")
    public ResponseMsg<QueryRespBean<HashMap>> searchPackageInfo(@RequestBody Map map) {
        ResponseMsg<QueryRespBean<HashMap>> responseMsg = new ResponseMsg<>();
        //分页
        String param = JSON.toJSONString(map.get("pageParameter"));
        PageParameter pageParameter = JSON.parseObject(param, PageParameter.class);
        //查询条件
        String searchParamsParams = JSON.toJSONString(map.get("searchParams"));
        HashMap searchParams = JSON.parseObject(searchParamsParams, HashMap.class);

        QueryReqBean<HashMap> queryReqBean = new QueryReqBean<>();
        queryReqBean.setSearchParams(searchParams);
        queryReqBean.setPageParameter(pageParameter);

        if (StringUtils.hasText(queryReqBean.getSidx()) && StringUtils.hasText(queryReqBean.getSord())) {
            String sordExp = getSortExpColumnName(queryReqBean.getSidx(), queryReqBean.getSord(), HashMap.class);
            if (StringUtils.hasText(sordExp)) {
                queryReqBean.setSortExp(sordExp);
            }
        }
        if (StringUtils.hasText(queryReqBean.getKeyWord())) {
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            HashMap searchKeyWordParam = JsonHelper.getInstance().readValue(queryReqBean.getKeyWord(), dateFormat, new TypeReference<HashMap>() {
            });
            queryReqBean.setSearchParams(searchKeyWordParam);
        }
        QueryRespBean<HashMap> result = msTaskPackageService.searchPackageMaterial(queryReqBean, ContextCommonHolder.getOrgId(), queryReqBean.getSortExp());
        List<HashMap> result1 = msTaskPackageService.searchPackageMaterial(searchParams, ContextCommonHolder.getOrgId(), queryReqBean.getSortExp());
        if (result1.size() == 0) {
            responseMsg.setRetCode("201").setMessage("未查询到数据").setResponseBody(result);
        } else responseMsg.setResponseBody(result);
        return responseMsg;
    }

    /*工作包物料信息插入*/
    @PostMapping("/insertPackageMaterialInfo")
    public ResponseMsg<Map> insertPackageMaterialInfo(@RequestBody HashMap map) {
        ResponseMsg<Map> responseMsg = new ResponseMsg<>();
        Map reMsg = new HashMap();
        int s = 0;
        int f = 0;
        try {
            msTaskPackageService.insertPackageMaterialInfo(map);
            s++;
        } catch (Exception e) {
            e.printStackTrace();
            f++;
        }
        reMsg.put("成功：", s);
        reMsg.put("失败：", f);
        if (s == 0 && f != 0) {
            responseMsg.setRetCode("201");
            responseMsg.setMessage("新增失败！");

        }
        if (s != 0 && f == 0) {
            responseMsg.setRetCode("200");
            responseMsg.setMessage("新增成功！");
        }
        return responseMsg;
    }

    /*工作包物料信息修改*/
    @PostMapping("/updatePackageMaterial")
    public ResponseMsg<Map> updatePackageMaterial(@RequestBody HashMap map) {
        ResponseMsg<Map> responseMsg = new ResponseMsg<>();
        Map reMsg = new HashMap();
        int s = 0;
        int f = 0;
        if (map.get("msPackageMaterialId") != null) {
            try {
                msTaskPackageService.updatePackageMaterial(map);
                s++;
            } catch (Exception e) {
                e.printStackTrace();
                f++;
            }
        } else return responseMsg.setRetCode("201").setMessage("请选中一条数据！");
        reMsg.put("成功：", s);
        reMsg.put("失败：", f);
        if (s == 0 && f != 0) {
            responseMsg.setRetCode("201");
            responseMsg.setMessage("修改失败！");

        }
        if (s != 0 && f == 0) {
            responseMsg.setRetCode("200");
            responseMsg.setMessage("修改成功！");
        }
        return responseMsg;
    }

    /*工作包物料信息删除*/
    @PostMapping("/deletePackageMaterial")
    public ResponseMsg<Map> deletePackageMaterial(@RequestBody HashMap map) {
        ResponseMsg<Map> responseMsg = new ResponseMsg<>();
        Map reMsg = new HashMap();
        int s = 0;
        int f = 0;
        List<String> ids = (List<String>) map.get("msPackageMaterialId");
        if (ids.size() == 0) {
            return responseMsg.setRetCode("201").setMessage("请选择需要删除的数据");
        }
        for (int i = 0; i < ids.size(); i++) {
            String msPackageMaterialId = ids.get(i);
            if (msPackageMaterialId != null) {
                msTaskPackageService.deletePackageMaterial(msPackageMaterialId);
                s++;
            } else f++;
        }
        reMsg.put("成功：", s);
        reMsg.put("失败：", f);
        if (s == 0 && f != 0) {
            responseMsg.setRetCode("201");
            responseMsg.setMessage("删除失败！");

        }
        if (s != 0 && f == 0) {
            responseMsg.setRetCode("200");
            responseMsg.setMessage("删除成功！");
        }
        if (s != 0 && f != 0) {
            responseMsg.setRetCode("202");
            responseMsg.setMessage("删除有失败项！");
            responseMsg.setResponseBody(reMsg);
        }
        return responseMsg;
    }

    //查询配套任务数量
    @PostMapping("/searchTaskCount")
    public ResponseMsg<String> searchTaskCount() {
        ResponseMsg responseMsg = new ResponseMsg();
        HashMap result = msTaskPackageService.searchTaskCount();
        if (result != null) return responseMsg.setResponseBody(result).setRetCode("200");
        else return responseMsg.setRetCode("201");
    }

    //查询分拣台任务数量信息
    @PostMapping("/searchSortTableCount")
    public ResponseMsg<String> searchSortTableCount() {
        ResponseMsg responseMsg = new ResponseMsg();
        HashMap map = new HashMap();
        String IP = null;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (request.getHeader("x-forwarded-for") == null) {
            IP = request.getRemoteAddr();
        } else {
            IP = request.getHeader("x-forwarded-for");
        }
        map.put("ip", IP);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        map.put("time", formatter.format(date));
        log.info("分拣任务时间:" + formatter.format(date));
        HashMap result = msTaskPackageService.searchSortTableCount(map);
        if (result != null) responseMsg.setResponseBody(result).setRetCode("200");
        else responseMsg.setRetCode("201").setMessage("暂无数据");
        return responseMsg;
    }

    /**
     * 查询当前分拣台对应工作包任务
     * 如果当前没有正在进行的任务，不展示ao。如果正在进行任务，显示当前操作工作包，当前操作ao
     *
     * @return
     */
    @PostMapping("/searchCurrentPackage")
    public ResponseMsg<HashMap> searchCurrentPackage() {
        ResponseMsg responseMsg = new ResponseMsg();
        String IP = null;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (request.getHeader("x-forwarded-for") == null) {
            IP = request.getRemoteAddr();
        } else {
            IP = request.getHeader("x-forwarded-for");
        }
        responseMsg.setResponseBody(msTaskPackageService.searchCurrentPackage(IP));
        return responseMsg;
    }

//    /**
//     * 查询当前分拣台选中的工作包对应AO
//     * @param map
//     * @return
//     */
//    @PostMapping("/searchCurrentAo")
//    public ResponseMsg<List<HashMap>> searchCurrentAo(@RequestBody Map map){
//        ResponseMsg responseMsg = new ResponseMsg();
//        String packageCode = (String) map.get("packageCode");
//        responseMsg.setResponseBody(msTaskPackageService.searchCurrentAo(packageCode));
//        return responseMsg;
//    }

    @PostMapping("/callSortBox")
    //分拣操作页面执行任务
    public ResponseMsg<String> callSortBox() {
        ResponseMsg responseMsg =null;
        String IP = null;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (request.getHeader("x-forwarded-for") == null) {
            IP = request.getRemoteAddr();
        } else {
            IP = request.getHeader("x-forwarded-for");
        }
//        //0724 lqc 修改操作台暂停状态
//        msTaskCheckOperDao.packageStart(IP);
//        //2023-03-09 yebo 查询当前是否已经执行分拣任务
//        HashMap checkMap = msTaskCheckOperService.checkTaskIsExe(IP);
//        if (!"0".equals(checkMap.get("cnt").toString())) {
//            responseMsg.setRetCode("201");
//            responseMsg.setMessage("分拣任务已经启动！");
//            return responseMsg;
//        }
        responseMsg = msTaskPackageService.callSortBoxSql(IP);
//        HashMap sqlRtrun = msTaskPackageService.callSortBoxSql(IP);
//        if (sqlRtrun != null && sqlRtrun.get("retCode").equals("200")) {
//            responseMsg.setRetCode("200");
//            responseMsg.setMessage("分拣任务启动成功！");
//            responseMsg.setResponseBody(sqlRtrun.get("callWCSInterface"));
//        } else {
//            responseMsg.setRetCode("201");
//            responseMsg.setMessage("分拣任务启动失败！");
//        }
        return responseMsg;
    }

    @PostMapping("searchAoByPackageCode")
    public ResponseMsg<List> searchAoByPackageCode(@RequestBody HashMap map) {
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setResponseBody(msTaskPackageService.searchAoByPackageCode(map));
        responseMsg.setMessage("操作成功");
        responseMsg.setRetCode("200");
        return responseMsg;
    }

    //分拣操作页面-查询ao下物料信息
    @PostMapping("/searchLog")
    public ResponseMsg<QueryRespBean<HashMap>> searchLog(@RequestBody HashMap map) {
        ResponseMsg<QueryRespBean<HashMap>> responseMsg = new ResponseMsg();
//        String IP = null;
//        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//        if (request.getHeader("x-forwarded-for") == null) {
//            IP = request.getRemoteAddr();
//        } else {
//            IP = request.getHeader("x-forwarded-for");
//        }
//        String AoId= (String) map.get("msTaskAoId");
        //查询条件
        String searchParamsParams = JSON.toJSONString(map.get("searchParams"));
        HashMap searchParams = JSON.parseObject(searchParamsParams, HashMap.class);
        String page = JSON.toJSONString(map.get("pageParameter"));
        PageParameter pageParameter = JSON.parseObject(page, PageParameter.class);
        QueryReqBean<HashMap> queryReqBean = new QueryReqBean<>();
        queryReqBean.setPageParameter(pageParameter);
        queryReqBean.setSearchParams(searchParams);
        QueryRespBean<HashMap> result = msTaskPackageService.searchMatchBill(queryReqBean);
        responseMsg.setResponseBody(result);
        return responseMsg;
    }

    //寻找下一个可以分拣的工作包
    @PostMapping("/sortTaskGoOn")
    public ResponseMsg sortTaskGoOn(@RequestBody HashMap map) {
        ResponseMsg responseMsg = new ResponseMsg();
        String IP = null;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (request.getHeader("x-forwarded-for") == null) {
            IP = request.getRemoteAddr();
        } else {
            IP = request.getHeader("x-forwarded-for");
        }

        return responseMsg;
    }

    @Transactional
    @PostMapping("/packageStay")
    public ResponseMsg packageStay() {
        ResponseMsg responseMsg = new ResponseMsg();
        String IP = null;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (request.getHeader("x-forwarded-for") == null) {
            IP = request.getRemoteAddr();
        } else {
            IP = request.getHeader("x-forwarded-for");
        }
        String status = msTaskCheckOperDao.searchConsole(IP);
        if ("2".equals(status)) {
            responseMsg.setMessage("当前操作台任务已暂停,请勿重复操作").setRetCode("201");
        } else {
            int i = msTaskCheckOperDao.packageStay(IP);
            if (i > 0) {
                responseMsg.setMessage("暂停成功").setRetCode("200");
            } else {
                responseMsg.setMessage("暂停失败").setRetCode("201");
            }
        }
        return responseMsg;
    }

    @Transactional
    @PostMapping("/cleanWaitingTask")
    public ResponseMsg cleanWaitingTask() {
        ResponseMsg responseMsg = new ResponseMsg();
        String IP = null;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (request.getHeader("x-forwarded-for") == null) {
            IP = request.getRemoteAddr();
        } else {
            IP = request.getHeader("x-forwarded-for");
        }
        HashMap location = msTaskCheckOperDao.searchLocation(IP);
        int result = 0;
        if (Objects.isNull(location)) {
            return responseMsg.setRetCode("201").setMessage("操作台信息有误");
        } else {
            result += msTaskCheckOperDao.delGkLog(location);
            result += msTaskCheckOperDao.deleteBigLog(location);//yebo0801 新增清理大件任务
            result += msTaskCheckOperDao.delGkLogSupple(location);
        }
        if (result <= 0) {
            responseMsg.setMessage("当前操作台任务无可清理任务").setRetCode("200");
        } else {
            responseMsg.setMessage("清理成功").setRetCode("200");
        }
        return responseMsg;
    }
}
